//
//  MvListViewController.m
//  CatMusic
//
//  Created by dllo on 15/4/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MvListViewController.h"
#import "MVListCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "MvPlayViewController.h"
#import "NightMode.h"
#import "AFNetworkReachabilityManager.h"
#import "PlayerSingleton.h"
#import "MJRefresh.h"
#import "NetWorkingToolMv.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MvListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    NSInteger number;
}
@property(nonatomic, retain)UICollectionView *mvListCV;

@end

@implementation MvListViewController

-(void)dealloc
{
    [_mvListCV release];
    [_arrMV release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrMV = [NSMutableArray array];
        number = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"MV精选";
    
    
    // 夜间模式判断
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLightDayModel) name:@"LightDayNSNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setNightDayModel) name:@"NightDayNSNotification" object:nil];
    
    if ([NightMode shareNightMode].isNight) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }

    
    
    UICollectionViewFlowLayout *mvListFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    mvListFlowLayout.itemSize = CGSizeMake(WIDTH / 375 * 160, 170);
    mvListFlowLayout.minimumInteritemSpacing = 10;
    mvListFlowLayout.minimumLineSpacing = 5;
    mvListFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.mvListCV = [[UICollectionView alloc] initWithFrame:CGRectMake(WIDTH / 375 * 10, 20, self.view.frame.size.width - 20, self.view.frame.size.height - 100) collectionViewLayout:mvListFlowLayout];
    self.mvListCV.backgroundColor = [UIColor clearColor];
    self.mvListCV.delegate = self;
    self.mvListCV.dataSource = self;
    [self.view addSubview:self.mvListCV];
    [self.mvListCV registerClass:[MVListCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    
#pragma mark - 判断网络状态    AFN
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                NSLog(@"未知网络");
                break;
                
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                NSLog(@"没有网络(断网)");
                [self alertViewCancel];
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
//                NSLog(@"手机自带网络");
                [self alertViewCancelAFNetworkReachabilityStatusReachableViaWWAN];
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                NSLog(@"WIFI");
                [self creatDataMv];
                [self mvListCVRefresh];
                break;
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];

    
    
}


- (void)creatDataMv
{
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/channel/channel/mvs?page=1&size=100"];
    [NetWorkingToolMv netWorkingToolStr:str block:^(id result) {
        NSMutableDictionary *dic = result;
        self.arrMV = [dic objectForKey:@"data"];
        [self.mvListCV reloadData];
    }];
    
}




// 下拉刷新
- (void)mvListCVRefresh
{
    [self.mvListCV addLegendHeaderWithRefreshingBlock:^{
        // 延迟加载数据，2秒后才调用
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.mvListCV reloadData];
            
            // 结束刷新
            [self.mvListCV.header endRefreshing];
        });
        
    }];
}



- (void)alertViewCancel
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络断开连接" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
    [alert show];
}

- (void)alertViewCancelAFNetworkReachabilityStatusReachableViaWWAN
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"手机自带网络" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
    [alert show];
}




-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrMV.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MVListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.baseTitleCollection.text = self.arrMV[indexPath.row][@"videoName"];
    NSString *str = self.arrMV[indexPath.row][@"bigPicUrl"];
    NSURL *url = [NSURL URLWithString:str];
    [cell.baseImageViewCollection sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolder"]];
    
//    cell.contentView.backgroundColor = [UIColor redColor];

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [[PlayerSingleton sharePlayerSingleton].player pause];
    MvPlayViewController *mvPlay = [[MvPlayViewController alloc] init];
    mvPlay.arr = self.arrMV;
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self.navigationController pushViewController:mvPlay animated:YES];
    [mvPlay release];
}






//// 滑动隐藏tabBar
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    self.tabBarController.tabBar.hidden = YES;
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    self.tabBarController.tabBar.hidden = NO;
//}


- (void)setLightDayModel
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.barTintColor = [UIColor colorWithRed:245 / 255.0 green:241/ 255.0 blue:143 / 255.0 alpha:1];//////
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:245 / 255.0 green:241/ 255.0 blue:143 / 255.0 alpha:1];//////
    
    UIColor * color = [UIColor blackColor];
    //这里我们设置的是颜色，还可以设置shadow等，具体可以参见api
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    //大功告成
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
   }

- (void)setNightDayModel
{
    self.view.backgroundColor = [UIColor colorWithRed:38 / 255.0 green:38/ 255.0 blue:38 / 255.0 alpha:1];
    
    self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:38 / 255.0 green:38/ 255.0 blue:38 / 255.0 alpha:1];
    
    UIColor * color = [UIColor whiteColor];
    //这里我们设置的是颜色，还可以设置shadow等，具体可以参见api
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    //大功告成
    self.navigationController.navigationBar.titleTextAttributes = dict;

    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
